package netty.w3cschool.hello;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class Client {

    public static void main(String[] args) throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();

        Bootstrap b = new Bootstrap();
        b.group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel channel) throws Exception {
                        channel.pipeline().addLast(new ClientHandler());
                    }
                });

        ChannelFuture cf = b.connect("127.0.0.1",8765).sync();
        cf.channel().writeAndFlush(Unpooled.copiedBuffer("777".getBytes()));
        cf.channel().writeAndFlush(Unpooled.copiedBuffer("888".getBytes()));
        Thread.sleep(1000);
        cf.channel().writeAndFlush(Unpooled.copiedBuffer("999".getBytes()));
        cf.channel().closeFuture().sync();
        group.shutdownGracefully();

    }
}
